﻿/*功能描述：绩效设置
作者：李周蓬
时间：2021-12-13
修改人：李周蓬
修改时间：2021-12-13*/
using HongStone.Domain.PerformanceModel;
using HongStone.Dto.PerformanceDto;
using HongStone.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace HongStone.API.Controllers
{
    [ApiExplorerSettings(GroupName = "Performance")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PerformanceController : ControllerBase
    {
        private readonly PerformanceBll _performanceBll;
        public PerformanceController(PerformanceBll performanceBll)
        {
            _performanceBll = performanceBll;
        }
        /// <summary>
        /// 显示考核任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowTheAssessmentTask()
        {
            return Ok(_performanceBll.ShowTheAssessmentTask());
        }
        /// <summary>
        /// 显示考核统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowAssessmentTotal()
        {
            return Ok(_performanceBll.ShowAssessmentTotal());
        }
        /// <summary>
        /// 添加考核任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTheAssessmentTask(TheAssessmentTask theAssessmentTask)
        {
            return Ok(_performanceBll.AddTheAssessmentTask(theAssessmentTask));
        }
        /// <summary>
        /// 修改考核任务
        /// </summary>
        /// <param name="theAssessmentTask"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateTheAssessmentTask(TheAssessmentTask theAssessmentTask)
        {
            return Ok(_performanceBll.UpdateTheAssessmentTask(theAssessmentTask));
        }
        /// <summary>
        /// 删除考核任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTheAssessmentTask(string id)
        {
            return Ok(_performanceBll.DelTheAssessmentTask(id));
        }
        /// <summary>
        /// 归档
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateGuiDang(string id)
        {
            return Ok(_performanceBll.UpdateGuiDang(id));
        }
        /// <summary>
        /// 显示部门内考核
        /// </summary>
        /// <param name="itemNumber"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowDeptAssessment(string itemNumber)
        {
            return Ok(_performanceBll.ShowDeptAssessment(itemNumber));
        }
        /// <summary>
        /// 提交考核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateKaoHe(string id)
        {
            return Ok(_performanceBll.UpdateKaoHe(id));
        }
        /// <summary>
        /// 显示考核项目
        /// </summary>
        /// <param name="name"></param>
        /// <param name="year"></param>
        /// <param name="deptid"></param>
        /// <param name="itemNumber"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowProjectAssessment(string name, int year, int deptid, string itemNumber)
        {
            return Ok(_performanceBll.ShowProjectAssessment(name, year, deptid, itemNumber));
        }
        /// <summary>
        /// 绑定部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult BindDept()
        {
            return Ok(_performanceBll.BindDept());
        }
        /// <summary>
        /// 添加考核项目
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddProjectAssessment(ProjectAssessment projectAssessment)
        {
            return Ok(_performanceBll.AddProjectAssessment(projectAssessment));
        }
        /// <summary>
        /// 修改考核项目
        /// </summary>
        /// <param name="projectAssessment"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateProjectAssessment(ProjectAssessment projectAssessment)
        {
            return Ok(_performanceBll.UpdateProjectAssessment(projectAssessment));
        }
        /// <summary>
        /// 删除考核项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelProjectAssessment(string id)
        {
            return Ok(_performanceBll.DelProjectAssessment(id));
        }
        /// <summary>
        /// 绑定建筑类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult BindBudingType()
        {
            return Ok(_performanceBll.BindBudingType());
        }
        /// <summary>
        /// 修改考核名称
        /// </summary>
        /// <param name="projectAssessment"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateAssessmentName(ProjectAssessment projectAssessment)
        {
            return Ok(_performanceBll.UpdateAssessmentName(projectAssessment));
        }
        /// <summary>
        /// 修改考核分类
        /// </summary>
        /// <param name="projectAssessment"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateAssessmentNames(ProjectAssessment projectAssessment)
        {
            return Ok(_performanceBll.UpdateAssessmentNames(projectAssessment));
        }
        /// <summary>
        /// 删除考核分类
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAssessmentNames(string name)
        {
            return Ok(_performanceBll.DelAssessmentName(name));
        }
        /// <summary>
        /// 删除考核名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelAssessmentName(string name)
        {
            return Ok(_performanceBll.DelAssessmentName(name));
        }
        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="year"></param>
        /// <param name="deptid"></param>
        /// <param name="itemNumber"></param>
        /// <param name="totalcount"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowKao(int year, int deptid, string itemNumber, int pageindex = 1, int pagesize = 3)
        {
            var totalcount = 0;
            var list = _performanceBll.ShowKao(year, deptid, itemNumber, out totalcount, pageindex, pagesize);
            return Ok(new { list, totalcount });
        }
        /// <summary>
        /// 显示项目进度
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowAssessSchedule(string itemNumber)
        {
            var totalcount = 0;
            var list=_performanceBll.ShowAssessSchedule(itemNumber,out totalcount);
            return Ok(new { list, totalcount });
        }
        /// <summary>
        /// 发起审批
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateShenPi(string ids)
        {
            return Ok(_performanceBll.UpdateShenPi(ids));
        }
        /// <summary>
        /// 重新提交考核
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateChongKaoHe(string id)
        {
            return Ok(_performanceBll.UpdateChongKaoHe(id));
        }
        /// <summary>
        /// 调整数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowTiao()
        {
            return Ok(_performanceBll.ShowTiao());
        }
        /// <summary>
        /// 显示部门内奖金计算
        /// </summary>
        /// <param name="itemnumber"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowJi(string itemnumber)
        {
            return Ok(_performanceBll.ShowJi(itemnumber));
        }
        /// <summary>
        /// 导出部门奖金分配
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcelJi()
        {
            var values = _performanceBll.ShowJi("");
            var zhuans = new List<Dictionary<string, string>>();
            List<ShowJiDto> data = (List<ShowJiDto>)values.Data;
            foreach (var item in data)
            {
                zhuans.Add(new Dictionary<string, string>
                {
                    { "编号",$"{item.RelationId}" },{ "部门ID",$"{item.DeptId}" },{ "部门名称",$"{item.DeptName}" },{ "部门内奖金计算ID",$"{item.DeptSalaryCountId}" },
                    { "考核任务",$"{item.ItemNumber}" },{ "平均月工资",$"{item.MonthAvg}" },{ "部门绩效",$"{item.DeptJiXiao}" },{ "部门奖金占比",$"{item.DeptZhanBi}" },
                    { "月工资倍数",$"{item.BeiShu}" },{ "项目奖金",$"{item.ProjectSalary}" },{ "项目奖金占比",$"{item.ProjectZhanBi}" },{ "上半年",$"{item.Shang}" }
                    ,{ "下半年",$"{item.Xia}" }
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuans);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "部门奖金分配.xlsx"
            };
        }
        /// <summary>
        /// 显示项目奖金调整
        /// </summary>
        /// <param name="itemnumber"></param>
        /// <param name="deptid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowTiaokk(string itemnumber, int deptid)
        {
            return Ok(_performanceBll.ShowTiaokk(itemnumber, deptid));
        }
        /// <summary>
        /// 部门内奖金计算
        /// </summary>
        /// <param name="deptid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowSalaryCount(string itemnumber)
        {
            return Ok(_performanceBll.ShowSalaryCount(itemnumber));
        }
        /// <summary>
        ///部门内奖金计算1
        /// </summary>
        /// <param name="deptid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowStaffSalaryCount(int deptid)
        {
            return Ok(_performanceBll.ShowStaffSalaryCount(deptid));
        }
        /// <summary>
        /// 分配详情
        /// </summary>
        /// <param name="itemnumber"></param>
        /// <param name="deptid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowTiaoXiang(string itemnumber, int deptid)
        {
            return Ok(_performanceBll.ShowTiaoXiang(itemnumber, deptid));
        }
        /// <summary>
        /// 年度总奖金分配显示
        /// </summary>
        /// <param name="deptid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowYearBonus(int deptid, int state)
        {
            return Ok(_performanceBll.ShowYearBonus(deptid, state));
        }
        /// <summary>
        /// 导出部门奖金计算
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcelSalaryUpdate()
        {
            var values = _performanceBll.ShowStaffSalaryCount(0);
            var zhuans = new List<Dictionary<string, string>>();
            List<DeptSalaryCountDto> data = (List<DeptSalaryCountDto>)values.Data;
            foreach (var item in data)
            {
                zhuans.Add(new Dictionary<string, string>
                {
                    { "编号",$"{item.DeptCountId}" },{ "员工ID",$"{item.StaffId}" },{ "员工名称",$"{item.StaffName}" },{ "考核任务",$"{item.ItemNumber}" },
                    { "基本工资",$"{item.Salary}" },{ "工作时间/月",$"{item.WorkTime}" },{ "平均月工资",$"{item.MonthAvg}" },{ "自评互评",$"{item.Ping}" },
                    { "排名",$"{item.Order}" },{ "部门经理评价",$"{item.ManagerPing}" },{ "经理评价排名",$"{item.ManagerOrder}" },{ "加权系数",$"{item.XiShu}" }
                    ,{ "部门内奖金计算值",$"{item.Sum}" },{ "项目奖金计算值",$"{item.Sums}" },{ "项目奖金经理调整",$"{item.ManagerTiao}" },{ "项目奖金调整值",$"{item.Tiao}" }
                    ,{ "优秀员工奖",$"{item.Yuan}" },{ "预发奖金",$"{item.Yu}" },{ "工资倍数",$"{item.Bei}" },{ "部门ID",$"{item.DeptId}" },{ "合计(含预发)",$"{item.He}" }
                    ,{ "合计",$"{item.Hes}" },{ "上半年",$"{item.Shang}" },{ "差值",$"{item.Cha}" },{ "下半年",$"{item.Xia}" }
                    ,{ "差值2",$"{item.Chas}" }

                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuans);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "部门奖金计算.xlsx"
            };
        }
        /// <summary>
        /// 查看考核名称
        /// </summary>
        /// <param name="staffid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowChaSalaryUpdate(int staffid)
        {
            return Ok(_performanceBll.ShowChaSalaryUpdate(staffid));
        }
    }
}
